<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {
	public function index(){
		if($this->session->userdata('website_id') == '' || $this->session->userdata('website_user') == ''){
			$this->load->helper(array('url'));
			redirect('/website/login', 'location', 301);
		}
		else{
			#$this->session->userdata('user_id').'|'.$this->session->userdata('user_name');
			$this->layout->setLayout('default');
			$this->layout->setTitle('Administrator | Home');
			$month = isset($_GET['date']) ? quotes_to_entities($_GET['date']) : date('Y-m');
			$getmonth = isset($_GET['date']) ? substr($_GET['date'],5,2) : date('m');
			$year = isset($_GET['year']) ? $_GET['year'] : date('Y');
			if ($year == "") $year = date('Y');
			$month = isset($_GET['month']) ? $year . '-'.str_pad(quotes_to_entities($_GET['month']), 2, "0", STR_PAD_LEFT) : date('Y-m');
				
			$web = $this->session->userdata('sms_code');
			if(isset($web)){				
				$pos = strpos($web,' ');
				if ($pos === false) {			
					$filter = " AND sms_code = '$web'";
					$filcode = " AND sms_code = '$web'";
				}else{
					$syn = substr($web, 0, $pos);
					$code = substr($web, $pos + 1, strlen($web));
					$filter = " AND sms_syntax = '$syn' AND sms_code = '$code'";
					$filcode = " AND sms_syntax = '$syn' AND sms_code = '$code'";
				}
				$filmonth =  " AND T.message_request = '$web' ";
				$filcode =   " AND T.message_request = '$web' ";
			}
			if (isset($_GET['date'])){
				$filter = $filter . " AND DATE_FORMAT(receive_date, '%Y-%m-%d') = '$month'";
				$filmonth =  $filmonth . " AND DATE_FORMAT(receive_date, '%Y-%m-%d') = '$month'";
			}else{
				$filter = $filter . " AND receive_date BETWEEN '" . $month."-01 00:00:00" . "' AND '" . $month."-31 23:59:59" . "'";
				$filmonth =  $filmonth . " AND T.receive_date BETWEEN '" . $month."-01 00:00:00" . "' AND '" . $month."-31 23:59:59" . "'";
			}
		
			// get earning data
			$field = "Count(T.phone)*4000 As subtotal";
			if ($web == "GN mkh198542782") $field = "Count(T.phone)*4200 As subtotal";
			//if ($web == "GN mkh198542776") $field = "Round(Count(T.phone)*0.7,0)*4000 As subtotal";
			$union = "";
			if ((strpos($web,"GQ APA") !== false) || (strpos($web,"GQ API") !== false)) { 
				$field = "Count(T.phone)*4800 As subtotal";
				$filmonth =  " AND (T.message_request like '$web%' or T.message_request = '$web') AND T.service_num = '8700' AND T.receive_date BETWEEN '" . $month."-01 00:00:00" . "' AND '" . $month."-31 23:59:59" . "'";
				$union = "";
				$filcode =   " AND T.message_request like '$web%' ";
			}
			if ($web == "GN mkh198542776") 
			{
				if ($year == "2012"){
					$field = "Round(Count(T.phone)*0.7,0)*4000 As subtotal";
				}else{
					$field = "Round(Count(T.phone)*0.5,0)*4000 As subtotal";
				}
				$filmonth1 =  " AND T.message_request = 'GN mkh198542776apk' AND T.receive_date BETWEEN '" . $month."-01 00:00:00" . "' AND '" . $month."-31 23:59:59" . "'";
				if ($year == "2012"){
					$field2 = "Round(Count(T.phone)*0.3,0)*4000 As subtotal";
				}else{
					$field2 = "Round(Count(T.phone)*0.5,0)*4000 As subtotal";
				}
				$union = " UNION SELECT DATE_FORMAT(T.receive_date, '%d-%m-%Y') AS earn_date, $field2 
FROM `tdx_sms_mo` T
WHERE  1=1 $filmonth1
Group by earn_date, telco, service_num, syntax, message_request";
				if ($year != "2012") $union = "";
			}
			if ($web == "GN mkh198542780") 
			{
				$field = "Count(T.phone)*4000 As subtotal";
				/*$filmonth1 =  " AND T.message_request = 'GN mkh198542780apk' AND T.receive_date BETWEEN '" . $month."-01 00:00:00" . "' AND '" . $month."-31 23:59:59" . "'";
				$union = " UNION SELECT DATE_FORMAT(T.receive_date, '%d-%m-%Y') AS earn_date,  Count(T.phone)*4000 As subtotal
FROM `tdx_sms_mo` T
WHERE  1=1 $filmonth1
Group by earn_date, telco, service_num, syntax, message_request";*/
				$filmonth =  " AND (T.message_request = '$web' or T.message_request = 'GN mkh198542780apk') AND T.receive_date BETWEEN '" . $month."-01 00:00:00" . "' AND '" . $month."-31 23:59:59" . "'";
				$union = "";
			}
			
			$sqlThisMonthEarning = "SELECT sms_ftp AS subtotal, 
									   DATE_FORMAT(receive_date, '%d-%m-%Y') AS earn_date
									FROM tdx_sms_stat WHERE 1=1 $filter";
			$sqlwebsite = "SELECT DATE_FORMAT(T.receive_date, '%d-%m-%Y') AS earn_date, $field
			FROM `tdx_sms_mo` T
			WHERE  1=1 $filmonth
			Group by earn_date, telco, service_num, syntax, message_request ";
			$sqlwebsite = $sqlwebsite . $union;
			$sqlThisMonthEarning = $sqlwebsite;
			$monthQuery = $this->db->query($sqlThisMonthEarning);
			if ($monthQuery->num_rows() > 0){
				$arrData = array();
				$total = 0;
				$intMonth = substr($monthQuery->row(0)->earn_date,0,2);
				$month2 = $getmonth;
				if (($getmonth < 10) && strlen($getmonth) == 1 ) $month2 = '0' . $getmonth;
				$Y = substr($monthQuery->row(0)->earn_date,6,4);
				$data['month'] = $month2 .'-'.$Y; 
				if ($intMonth > 1){					
					for ($i=1;$i<=$intMonth - 1;$i++):					
						if ($i < 10)
							$arrData['0'. $i . '-' . $month2 . '-' . $Y] = 0;
						else
							$arrData[$i . '-' . $month2 .'-'. $Y] = 0;
					endfor;
					$dd = $i . '-' . $month2 .'-'. $Y;
				}
				foreach ($monthQuery->result_array() as $row){
					$arrData[$row['earn_date']] += intval($row['subtotal']);
					$total += intval($row['subtotal']);
				}
			}
			if(isset($_GET['date'])){
				$today = $_GET['date'];
				$field = "Count(T.phone)*4000 As subtotal";
				if ($web == "GN mkh198542782") $field = "Count(T.phone)*4200 As subtotal";
				if ($web == "GN mkh198542776") $field = "Round(Count(T.phone)*0.7,0)*4000 As subtotal";	
				$sqlToday = "SELECT $field
									FROM tdx_sms_mo T WHERE T.receive_date BETWEEN '" . $today." 00:00:00" . "' AND '" . $today." 23:59:59" . "'" . $filcode;
				
				$todayQuery = $this->db->query($sqlToday);
				$todayData = $todayQuery->row();
				$strDate = 'Ngày '.$_GET['date'];
				$data['earn_today'] = intval($todayData->subtotal);
				
				$arrDate = explode("-", $today);
				$month1 = $arrDate[0].'-'.$arrDate[1];
				$data['month1'] = $arrDate[1].'-'.$arrDate[0];
				$filcode = $filcode . " AND T.receive_date BETWEEN '" . $month1 ."-01 00:00:00" . "' AND '" . $month1 ."-31 23:59:59" . "'";
				$sqlThisMonthEarning1 = "SELECT $field FROM tdx_sms_mo T WHERE 1=1 $filcode";
				$monthQuery1 = $this->db->query($sqlThisMonthEarning1);
				$monthData1 = $monthQuery1->row();
				$total = intval($monthData1->subtotal);
			}
			else{
				$strDate = 'Hôm nay('.date('d-m-Y').')';
				$data['earn_today'] = intval($arrData[date('d-m-Y')]);
			}
			$data['earn_yesterday'] = intval($arrData[date('d-m-Y', strtotime('-1 day'))]);
			$data['earn_this_month'] = $total;
			$data['str_date'] = $strDate;
			$data['month_data'] = $arrData;
			
			$this->layout->view(THEME_DIR.'/home_index', $data);
		}
	}
}